<?php

function my_encrypt($var,$salt){
     return sha1($var.$salt);
}

function purify($var){
	return trim(strip_tags(nl2br($var)));
}

function clean($var){
	$var = strip_tags($var);
	return auto_typography($var);
}

function set_url_title($var){
	$var = word_limiter($var,10,'');
	$var = strtolower($var);
	$var = trim($var);	
    return url_title($var,'dash',TRUE);
}

function show_dialog($status,$title,$message,$button,$action){
	$CI =& get_instance(); 
	$CI->session->set_flashdata('dialog','yes');
	$CI->session->set_flashdata('dialog_status',$status);	
	$CI->session->set_flashdata('dialog_title',$title);	
	$CI->session->set_flashdata('dialog_message',$message);
	$CI->session->set_flashdata('dialog_button',$button);
	$CI->session->set_flashdata('dialog_action',$action);	
}

function get_age($_dob) {
    $dob = date("Y-m-d",strtotime($_dob));
    $ageparts = explode("-",$dob);

    $age = date("Y-m-d")-$dob;

    return (date("nd") < $ageparts[1].str_pad($ageparts[2],2,'0',STR_PAD_LEFT)) ? $age-=1 : $age;
}

function time_since($original) {
	
	$original = strtotime($original);
	$chunks = array(
			array(60 * 60 * 24 * 365 , 'year'),
			array(60 * 60 * 24 * 30 , 'month'),
			array(60 * 60 * 24 * 7, 'week'),
			array(60 * 60 * 24 , 'day'),
			array(60 * 60 , 'hour'),
			array(60 , 'minute'),
	);
	
	$today = time();
	$since = $today - $original;

	if($since > 604800) {
		$print = date("M jS", $original);
		if($since > 31536000) {
			$print .= ", " . date("Y", $original);
		}
		return $print;
	}
	
	for ($i = 0, $j = count($chunks); $i < $j; $i++) {
		$seconds = $chunks[$i][0];
		$name = $chunks[$i][1];
		
		if (($count = floor($since / $seconds)) != 0) {
				break;
		}
	}

	$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
	return $print . " ago";
}

function format_long_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%A, %d %B %Y", mktime(0, 0, 0, $m, $d, $y));
}

function format_short_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%d.%m.%y", mktime(0, 0, 0, $m, $d, $y));
}

function format_medium_date($date){
	$y = substr($date,0,4);
	$m = substr($date,5,2);
	$d = substr($date,8,2);
	return strftime("%d %B %Y", mktime(0, 0, 0, $m, $d, $y));
}

function format_hour($date){
	$h = substr($date,0,2);
	$m = substr($date,3,2);
	$s = substr($date,6,2);
	return strftime("%H:%M", mktime($h, $m, $s, 0, 0, 0));
}

function get_ip_address(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }else{
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

function set_select_month()
{
	// Month Array
	$months = array(
		"01"=>"January",
		"02"=>"February",
		"03"=>"March",
		"04"=>"April",
		"05"=>"May",
		"06"=>"June",
		"07"=>"July",
		"08"=>"August",
		"09"=>"September",
		"10"=>"October",
		"11"=>"November",
		"12"=>"December"
	);
	
	return $months;
}

function set_select_date()
{
	$date = array();
	for($i=1; $i<=31; $i++)
	{
		if($i<10) $i = "0".$i;
		$date[] = $i;
	}	
	return $date;
}

function set_select_year($type="")
{
	$year = array();
	if($type=="latest")
	{
		$begin = date('Y')-1;
		$end = date('Y')+1;
	}
	else
	{
		$begin = 2000;
		$end = 2011;
	}
	
	for($i=$begin; $i<=$end; $i++)
	{
		$year[] = $i;
	}
	return $year;
}


function is_day_valid($d,$m,$y)
{
	if($d <> '' && $m <> '' && $y <> '')
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}

function notification($type,$message)
{
	switch($type)
	{
		case 'success':
			$notif = '<div class="notification notification-success">'.$message.'</div>';
			break;
		case 'error':
			$notif = '<div class="notification notification-error">'.$message.'</div>';
			break;
	}
	
	return $notif;
}

function set_url_youtube($url)
{
	//$url = prep_url(trim($url));
	$url = str_replace('watch?v=','v/',$url);
	return $url;
}

function get_youtube_image($url='')
{
	$is_youtube = strpos($url,"youtube") ? TRUE : FALSE;

	if($url<>'' && $is_youtube){
		$url = str_replace('www','img',$url);
		$url = str_replace('v/','vi/',$url);
		$url .= '/2.jpg';
		return $url;
	}
	else
	{
		return '';
	}
	
}

function print_r_with_pre($vars)
{
	return '<pre>'.print_r($vars).'</pre>';
}

function grab_label_image($label)
{
	$image = '';
	switch($label)
	{
		case 1: 
			$image = base_url()."images/ic-st-new.png";
		break;
		case 2:
			$image = base_url()."images/ic-st-sale.png";
		break;
		case 3:
			$image = base_url()."images/ic-st-sold.png";
		break;
	}
	
	return $image;
}

function grab_thumbs_product($img='')
{
	if($img <> ''){
		$image = base_url()."uploads/product/thumbs/tn_".$img;
	}else{
		$image ='';
	}
	return $image;
}

function grab_banner_image($img='')
{
	if($img <> ''){
		$image = base_url()."uploads/banner/".$img;
	}else{
		$image ='';
	}
	return $image;
}

function grab_price($price, $disc)
{
	if($disc > 0){
		$totalPrice = ($price * $disc) / 100;
		$tag = '<span class="line">IDR '.number_format($price, 0, '', ',').'</span>';
		$tag.= ' &rsaquo; ';
		$tag.= '<span class="idr">IDR '.number_format($totalPrice, 0, '', ',').'</span>';
	}else{
		$tag = '<span class="idr">IDR '.number_format($price, 0, '', ',').'</span>';
	}
	return $tag;
}
?>
